
class GYSeqImage extends Laya.Sprite
{		
	public static default_interval:number = 100;
	protected __seqArr:Array<any>;
	protected __seqIndex:number;
	protected __startTime:number;
	protected __intervalTime:number;
	protected __isRunning:number;
	protected __startIndex:number;
	protected __waitSource:any;
	protected __sourceWaitRate:number;
	protected __loopTimes:number;
	protected __reserveEnd:Boolean;
	private __reserving:Boolean;
	protected __loops:number;
	protected __frameNum:number;
	private __endFunc:Function;
	private __updateFunc:Function;
	private __endObject:any;
	private __updateObject:any;
	private __endIndex:number;
	private __source:Laya.Texture[];
	/**结束回调，参数为已播放几次,s.endFunc(loopTimes)*/public endFunc:Function;

	public get seqArr():Array<any>
	{
		return this.__seqArr;
	}

	public get seqIndex():number
	{
    var s = this;
		return s.__seqIndex;
	}

	public get frameNum():number
	{
		return this.__frameNum;
	}
	
	public constructor()
	{
		super();
		var s = this;
		s.initComponent();
	}
	protected initComponent():void
	{
    	var s = this;
		s.__seqArr=null;
		s.__seqIndex=0;
		s.__startTime=0;			
		s.__isRunning=0;
		s.__startIndex=0;
		s.__waitSource=null;			
		s.__loopTimes=0;
		s.__intervalTime = GYSeqImage.default_interval;
		s.__sourceWaitRate = -1;
		s.__reserveEnd = false;
		s.__reserving = false;
		s.__loops = Number.MAX_VALUE;
		s.__frameNum = 0;
		s.__endFunc = null;
		s.__updateFunc = null;
		s.__endObject = null;
		s.__updateObject = null;
		s.__endIndex = Number.MAX_VALUE;
	}
	public get source():Laya.Texture[]
	{
		return this.__source;
	}
	
	/**结束回调，
	 * val endFunc(loopTimes) 参数为已播放几次,
	 * */
	public setEndFunc(val:Function,thisObject:any):void
	{
		var s = this;
		s.__endFunc = val;
		s.__endObject = thisObject;
	}
	/**更新回调
	 * val endFunc(index) 参数为当前第几帧
	 * */
	public setUpdateFunc(val:Function,thisObject:any):void
	{
		var s = this;
		s.__updateFunc = val;
		s.__updateObject = thisObject;
	}
	
	public set source(bmp:Laya.Texture[])
	{
		let s= this;
		if(bmp == s.__source)
			return;
		if(s.__sourceWaitRate > -1)
		{
			if(s.__isRunning == 1)
			{
				s.__waitSource = bmp;
				return;
			}
		}
    	if(s.__isRunning == 0)
			s.start(bmp,s.__seqIndex);
		else
			s.reset(bmp,s.__seqIndex);
	}
	/**序列帧播放
	 * @ind 起始索引
	 * */
	public start(seq:Array<any>, seqInd:number=0):void
	{
    	var s = this;
		if(seq == null && s.__seqArr == null)return;
		s.reset(seq,seqInd);
		if(s.__isRunning == 1)return;
		s.__isRunning = 1;
		CommonUtil.addStageLoop(s.loop,s);
	}
	private reset(seq:Array<any>, seqInd:number=0):void
	{
		let s = this;
		if(seq)s.__seqArr = seq;
		s.__frameNum = s.__seqArr.length;
		s.__startTime = CommonUtil.loopTime;
		s.__seqIndex = s.__startIndex = seqInd<s.__frameNum?seqInd:s.__frameNum-1;
		s.__loopTimes = 0;
		s.sourceWaitRate = s.__sourceWaitRate;			
		s.texture = s.__seqArr[s.__seqIndex];
		// GYSprite.superSetter(GYSeqImage, s, "source", s.__seqArr[s.__seqIndex]);
	}		/**停止序列帧
		* @ind 如果-1，这清理序列帧 如果是大于-1，则停止在ind索引的图片显示
		* */
	public stop(ind:number=-1):void
	{
    	var s = this;
		if(s.__isRunning == 1)
		{
			CommonUtil.delStageLoop(s.loop,s);
		}			
		if(ind == -1)
		{
			s.__isRunning = 0;
			s.__seqArr = null;
			s.texture = null;
			return;
		}
		s.__isRunning = 2;
		if(s.__seqArr)
		{
			s.__seqIndex = ind < s.__frameNum?ind:s.__frameNum - 1;
			s.texture = s.__seqArr[s.__seqIndex];
		}	
	}
	public pause():void
	{
		this.stop(this.__seqIndex);
	}
	private loop(t:number):void
	{
    	var s = this;
		t = t - s.__startTime;
		var i:number,ls:number;
		i = (s.__startIndex + t / s.__intervalTime>>0);
		ls = i / s.__frameNum >> 0;
		if(ls != s.__loopTimes)
		{
			s.__reserving = !s.__reserving;
			s.__loopTimes= ls;
			if(ls >= s.__loops)
			{
				s.stop(s.__endIndex);
				if(s.__endFunc!=null)s.__endFunc.call(s.__endObject,s.__loopTimes);		
				return;
			}
			if(s.__endFunc!=null)s.__endFunc.call(s.__endObject,s.__loopTimes);				
		}
		i %=s.__frameNum;
		if(s.__reserving && s.__reserveEnd)
		{
			i = s.__frameNum -  i - 1;
		}
		if(i == s.__seqIndex)return;
		if(s.__updateFunc!=null)s.__updateFunc.call(s.__updateObject,i);
		s.__seqIndex = i;
		if(s.__waitSource && s.__seqIndex == s.__sourceWaitRate)
		{
			s.source = s.__waitSource;
			s.__waitSource = null;
			return;
		}
		s.texture = s.__seqArr[s.__seqIndex];
		// s.validDraw();
		// s.__mySource.smoothing = s.__smoothing;
	}
	/**序列帧间隔(毫秒)* */
	public get intervalTime():number
	{
    	var s = this;
		return s.__intervalTime;
	}

	public set intervalTime(value:number)
	{
    	var s = this;
		s.__intervalTime = value;
	}

	public get isRunning():number
	{
    	var s = this;
		return s.__isRunning;
	}
	/**切换图片时需要达到的帧数（当是序列帧动画） */
	public get sourceWaitRate():number
	{
    	var s = this;
		return s.__sourceWaitRate;
	}

	public set sourceWaitRate(value:number)
	{
    	var s = this;
		if(s.__sourceWaitRate == value)return;
		s.__sourceWaitRate = value;
		if(s.__seqArr && s.__sourceWaitRate >= s.__frameNum)
			s.__sourceWaitRate  = s.__frameNum - 1;
	}

	public get reserveEnd():Boolean
	{
    	var s = this;
		return s.__reserveEnd;
	}

	public set reserveEnd(value:Boolean)
	{
    	var s = this;
		s.__reserveEnd = value;
	}
	/**循环多少次停止*/
	public get loops():number
	{
    	var s = this;
		return s.__loops;
	}

	public set loops(value:number)
	{
    	var s = this;
		s.__loops = value;
	}
}
